import type { MenuTable } from '@/types/menu'
import type { FormRules } from 'element-plus'
import { ref, type Ref } from 'vue'

// 组件路径列表
export const componentList = ref(
  (() => {
    let keys = Object.keys(import.meta.glob('@/views/**/*.vue'))
    return keys.map((d: string) => {
      return {
        value: d.replace('.vue', '').replace('/src/views/', '')
      }
    })
  })()
)
export function componentSelect(queryString: string, cb: any) {
  const results = queryString
    ? componentList.value.filter((restaurant: any) => {
        return restaurant.value.toLowerCase().indexOf(queryString.toLowerCase()) === 0
      })
    : componentList.value
  cb(results)
}

export const formRules: Ref<FormRules<MenuTable>> = ref<FormRules<MenuTable>>({
  path: [
    {
      required: true,
      message: '请输入菜单导航路径'
    }
  ],
  key: [
    {
      required: true,
      message: '请输入路由名称(全局唯一)'
    }
  ],
  name: [
    {
      required: true,
      message: '请输入菜单名称'
    }
  ]
})
